package com.yiwei.zhh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.yiwei.zhh.R;


/**
 * Created by Administrator on 2017/3/29 0029.
 */

public class LeftIconText extends View {
    private String textContent = "";
    private int iconLeft;
    private int iconWidth;
    private int iconHeight;
    private int textSize = 14;
    private int textColor;
    private Paint textPaint;
    private Bitmap bitmap;
    private int textPaddingLeft;
    private Paint.FontMetricsInt fm;
    private Rect ract;

    public LeftIconText(Context context) {
        super(context);
        init(context, null);
    }

    public LeftIconText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    public LeftIconText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LeftIconText);
        textContent = a.getString(R.styleable.LeftIconText_rightText);
        iconLeft = a.getResourceId(R.styleable.LeftIconText_my_leftIcon, NO_ID);
        iconWidth = a.getDimensionPixelSize(R.styleable.LeftIconText_iconWidth, 30);
        iconHeight = a.getDimensionPixelSize(R.styleable.LeftIconText_iconHeight, 30);
        textSize = a.getDimensionPixelSize(R.styleable.LeftIconText_right_text_size, 14);
        textColor = a.getColor(R.styleable.LeftIconText_right_text_color, Color.BLACK);
        textPaddingLeft = a.getDimensionPixelSize(R.styleable.LeftIconText_textPaddingLeft, 5);
        a.recycle();

        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);
        bitmap = BitmapFactory.decodeResource(getResources(), iconLeft);
//        bitmap = Bitmap.createScaledBitmap(bitmap, iconWidth, iconHeight, true);
        fm = textPaint.getFontMetricsInt();
        ract = new Rect();
    }

//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        int widthWeasure = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
//        int heightMeasure = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
//        super.onMeasure(widthWeasure, heightMeasure);
//    }


    public void setAll(int resId, int textColor) {
        bitmap = BitmapFactory.decodeResource(getResources(), resId);
        textPaint.setColor(textColor);
        invalidate();
    }

    public void setData(int resId, String text) {
        bitmap = BitmapFactory.decodeResource(getResources(), resId);
        textContent = text;
        invalidate();
    }


    public static int getDefaultSize(int size, int measureSpec) {
        int result = size;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = size;
                break;
            case MeasureSpec.AT_MOST:

            case MeasureSpec.EXACTLY:
                result = specSize;
                break;
        }
        return result;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        canvas.drawBitmap(bitmap, getPaddingLeft(), getMeasuredHeight() / 2 - iconHeight / 2, textPaint);
        ract.set(getPaddingLeft(), getMeasuredHeight() / 2 - iconHeight / 2, getPaddingLeft() + iconWidth, getMeasuredHeight() / 2 - iconHeight / 2 + iconHeight);
        canvas.drawBitmap(bitmap, null, ract, textPaint);

        canvas.drawText(textContent, getPaddingLeft() + iconWidth + textPaddingLeft, getMeasuredHeight() / 2 - fm.descent + (fm.bottom - fm.top) / 2, textPaint);

    }
}
